Software Tools for Earth and Environmental Science

–7th WEEK–

Emir Toker

04/12/2020

R Language - Part 3

  • Syllabus, Last Week and Book

  • R Language - Part 3

    • Read
    • Write
    • Plot
  • TakeHome - MidTerm Project

  • Next Week - R Programming

Syllabus, Last Week and Book

Syllabus

Extended Syllabus PDF

LINK

Book

PDF - (Pg. 127-133 and 150-155)

Homework - I and II

*due date: 07/12/2020 23:59

Homework - I

Create a Notebook

Homework - II

Practive - Data Types and Structures

R Language

R Language - Part 1 & Part 2

  • Basic Math, Assigment, Comment
  • Data Types - Classes
    • Numeric
    • Integer
    • Logical
    • Character
    • Special Values
  • Data Structures - Objects
    • Vector
    • Matrice
    • Array
    • Data Frame
    • List

R Language - Part 3

  • Read

  • Write

  • Plot

R Language - Part 1 & 2

Getting Started

  • Assignment; <-
  • Comment; #
  • Help; ?func .or. help(func)
  • Install Packages; install.packages()
  • Call from Library; library()
  • Basic Math;
    • addition; +
    • subtraction; -
    • multiplication; *
    • division; /
    • exponentiation; ^
    • the square root; sqrt

Data Types - Classes

  • Numeric
# Any number with (or without) a decimal point.
a <- 3
  • Integer
# Sub-class of the numeric class. The suffix L tells R to store.
a <- 3L
  • Logical
# TRUE or FALSE - Logical Operators. < , > , == , >= , <= , != ... 
a <- 3<2
  • Character
# Data type consists of letters or words. String. with quotes: " … "
a <- "3"

is.XXX() and class()

Coercion

Data Structures (R-Objects)

Data Structures (R-Objects)

Data Structures - (Atomic) Vector

Vector : The simplest data structure in R

name <- "emir"
surname <- "toker"

print(c(name,surname))     # c means “combine”

Data Structures - Matrice

Vectors indexed using two indices instead of one.

[ row, col ]

a <- c(1:3)
# str(a) and dim(a) and length(a)
b <- matrix(1:3, nrow = 1, ncol = 3)
# str(b) and dim(b) and length(b)

Data Structures - Matrix

a <- c(1:3)
b <- matrix(1:3, nrow = 1, ncol = 3)
## [1] 1 2 3
##      [,1] [,2] [,3]
## [1,]    1    2    3
c <- matrix(1:9, nrow = 3, ncol = 3)
##      [,1] [,2] [,3]
## [1,]    1    4    7
## [2,]    2    5    8
## [3,]    3    6    9
d <- matrix(1:9, nrow = 3, ncol = 3, byrow = TRUE)
##      [,1] [,2] [,3]
## [1,]    1    2    3
## [2,]    4    5    6
## [3,]    7    8    9

Data Structures - Array

str(arr)
##  int [1:4, 1:3, 1:2] 1 2 3 4 5 6 7 8 9 10 ...
# dim(arr)
# length(arr)
x <- 1:24
##  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
arr <- array(x, dim = c(4,3,2)) #raw,col,level
## , , 1
## 
##      [,1] [,2] [,3]
## [1,]    1    5    9
## [2,]    2    6   10
## [3,]    3    7   11
## [4,]    4    8   12
## 
## , , 2
## 
##      [,1] [,2] [,3]
## [1,]   13   17   21
## [2,]   14   18   22
## [3,]   15   19   23
## [4,]   16   20   24

Data Structures - Array

[ row, col, level ]

Data Structures - Data Frame

  • Each element is of the same length, like a matrix.
  • A column can have different types.
  • BUT, all the elements within a column are the same type.

Data Structures - List

  • Lists are like atomic vectors because they group data into a one-dimensional set.
  • Lists are like data frame because they can group different types of data.
  • BUT, the length of elements is NOT important.

Data Structures - List

matrix <- matrix(data=1:4,nrow=2,ncol=2)
vector <- c(T,F,T,T)
var <- "hello"
data_frame <- new_df2

list  <- list(matrix,vector,var,data_frame)
class(list)
str(list)
dim(list)
length(list)

R Language - Part 3

R Language - Part 3

  • Read

  • Write

  • Plot

Read

library(help="datasets")
list.files("/Users/emirtoker/Desktop/Dersler/Memurluk/Software_Tools_for_Earth_&_Environmental_Science/Software_Tools_R_Github/Presentation")
file.choose()
read.table(file = "18397_Cekmekoy_Omerli_15dk.txt")

read.table(file = "18397_Cekmekoy_Omerli_15dk.txt", 
          header=TRUE, sep=";")
          
read.table(file = "18397_Cekmekoy_Omerli_15dk.txt", 
          header=TRUE, sep=";", na.strings="-9999")

mydata_txt <- read.table(file = "18397_Cekmekoy_Omerli_15dk.txt",
                        header=TRUE, 
                        sep=";",
                        na.strings="-9999")
              
str(mydata_txt)
mydata_csv <- read.csv(file="18397_Cekmekoy_Omerli.csv",
                      header=TRUE,
                      na.strings="-9999")
                      
str(mydata_csv)

Read and Write

url <- "https://web.itu.edu.tr/tokerem/18397_Cekmekoy_Omerli_15dk.txt"
urldata_txt <- read.table(url,
                          header=TRUE, 
                          sep=";",
                          na.strings="-9999")

Write .TXT and .CSV

write.table(x=urldata_txt,file="somenewfile.txt")

write.table(x=urldata_txt,file="somenewfile.txt",
           sep=";",na="-9999",quote=FALSE,row.names=FALSE)

new_df2
write.table(x=new_df2,file="dffile.txt",
            sep=";",na="-9999",quote=FALSE,row.names=FALSE)
            
write.table(x=new_df2,file="dffile.csv",
            sep=";",na="-9999",quote=FALSE,row.names=FALSE)

Plot

foo <- c(1.1,2,3.5,3.9,4.2)
bar <- c(2,2.2,-1.3,0,0.2)
plot(foo,bar)
  • type the supplied coordinates (for example, as stand-alone points or joined by lines or both dots and lines).
  • main, xlab, ylab Options to include plot title, the horizontal axis label, and the vertical axis label, respectively.
  • col Color (or colors) to use for plotting points and lines.
  • lty Stands for line type. (for example, solid, dotted, or dashed).
  • lwd This controls the thickness of plotted lines.
  • xlim, ylim limits for the horizontal range and vertical range (respectively)

Plot

plot(foo,bar)
plot(foo,bar,type="l")
plot(foo,bar,type="b",main="My lovely plot",xlab="x axis label", ylab="location y")
plot(foo,bar,type="b",main="My lovely plot",xlab="",ylab="",col="red")

x <- 1:20
y <- c(-1.49,3.37,2.59,-2.78,-3.94,-0.92,6.43,8.51,3.41,-8.23,
-12.01,-6.58,2.87,14.12,9.63,-4.58,-14.78,-11.67,1.17,15.62)
plot(x,y,type="n",main="")
abline(h=c(-5,5),col="red",lty=2,lwd=2)
segments(x0=c(5,15),y0=c(-5,-5),x1=c(5,15),y1=c(5,5),col="red",lty=3,
lwd=2)
points(x[y>=5],y[y>=5],pch=4,col="darkmagenta",cex=2)
points(x[y<=-5],y[y<=-5],pch=3,col="darkgreen",cex=2)
points(x[(x>=5&x<=15)&(y>-5&y<5)],y[(x>=5&x<=15)&(y>-5&y<5)],pch=19,
col="blue")
points(x[(x<5|x>15)&(y>-5&y<5)],y[(x<5|x>15)&(y>-5&y<5)])
lines(x,y,lty=4)
arrows(x0=8,y0=14,x1=11,y1=2.5)
text(x=8,y=15,labels="sweet spot")
legend("bottomleft",
legend=c("overall process","sweet","standard",
"too big","too small","sweet y range","sweet x range"),
pch=c(NA,19,1,4,3,NA,NA),lty=c(4,NA,NA,NA,NA,2,3),
col=c("black","blue","black","darkmagenta","darkgreen","red","red"),
lwd=c(1,NA,NA,NA,NA,2,2),pt.cex=c(NA,1,1,2,2,NA,NA))

Plot

mydata_txt <- read.table(file = "18397_Cekmekoy_Omerli_15dk.txt",
                        header=TRUE, 
                        sep=";",
                        na.strings="-9999")

mydata_txt

plot(mydata_txt$temp, type="l" )

Workshop - Midterm Project

Workshop - Midterm Project

  • Open a new R notebook
  • Go to course home page, (Midterm Project)
  • Click Rmd and Open “Midterm_Project.Rmd”
  • Copy all code and paste in your R notebook
  • Same way, open “18397_Cekmekoy_Omerli_15dk.txt” and paste file in your project directory.
  • Start to follow Instructions

Next Week (not next, the other one)

Next Week (not next, the other one)